
<%@page import="model.Imovel"%>
<%@page import="java.util.Iterator"%>
<%@page import="java.util.Map"%>
<%@page import="model.BD_Sistema"%>
<%@page import="model.Usuario"%>
<%@page import="java.util.HashMap"%>
<%@page contentType="text/html" pageEncoding="UTF-8"%>

<!DOCTYPE html>
<html>
    <head>

        <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
        <title>Home</title>
    </head>
    <body>
    <center>
        <%  String u_login = request.getParameter("login");
            String u_senha = request.getParameter("senha");
            Usuario u = new Usuario();
            String sessao = (String) session.getAttribute("sessao_atual");
            boolean valido;

            BD_Sistema bds = BD_Sistema.getInstance();
            bds.setBd_usuario((HashMap) session.getAttribute("usuario"));

            if (bds.getBd_usuario() == null || (!bds.checaUsuario(u_login) && sessao == null))
                response.sendRedirect("index.jsp");
            else {
                u = bds.buscaUsuario(u_login);
                if (sessao != null || u.getSenha().equals(u_senha)) {
                    valido = true;
                    if (sessao != null) {
                        u = bds.buscaUsuario(sessao);
                    }

                    if (valido) {
                        session.setAttribute("sessao_atual", u.getLogin());

        %>

        <h1> Ola <% out.print(u.getNome());%> </h1>

        <table>
            <tr>
                <td width="200px"><a href="imoveis_meus.jsp"> Meus imoveis</a></td>
                <td width="200px"><a href="register.jsp">Editar meus dados</a></td>
                <td width="200px"><a href="index.jsp">Sair</a></td></tr>
        </table>


        <%  bds.setBd_imovel((HashMap) session.getAttribute("imoveis"));
            Imovel i = new Imovel();
            if (bds.getBd_imovel() != null) {
                for (Map.Entry e : bds.getBd_imovel().entrySet()) {
                    i = (Imovel) e.getValue();
                    String link = "imoveis_detalhes.jsp?id=" + e.getKey();
        %>
        <table>
            <tr>
                <td>Categoria:</td>
                <td><% out.print(i.getCategoria());%></td>
            </tr>
            <tr>
                <td>Tipo:</td>
                <td><% out.print(i.getTipo());%></td>
            </tr>
            <tr>
                <td>Endereco:</td>
                <td><% out.print(i.getLogradouro());%></td>
            </tr>
            <tr>
                <td>Numero de quartos:</td>
                <td><% out.print(i.getNroQuartos());%></td>
            </tr>
            <tr>
                <td>Valor: <% out.print(i.getValor());%> </td>
            </tr>
            <tr>               
                <td colspan="2">
                    <form action="imoveis_detalhes.jsp">
                        <input type="hidden" name="id" value=<%= i.getId_imovel()%> >
                        <input type="submit" name="detalhar" value="Mais detalhes">
                    </form>
                </td>
            </tr>
            <tr>
                <td colspan="2">
                    <hr>
            </tr>
        </table>
        <%
                }
            }
        %>

        <%          } else {
                        response.sendRedirect("index.jsp");
                    }
                } else {
                    response.sendRedirect("index.jsp");
                }
            }
        %>
    </center>
</body>
</html>

